package com.conferenceroom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.conferenceroom.dto.AjaxResult;
import com.conferenceroom.dto.RoomSelect;
import com.conferenceroom.entity.Room;
import com.conferenceroom.service.RoomService;
import com.conferenceroom.vo.HourRoom;
import com.conferenceroom.vo.NowRoom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/rooms")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @GetMapping
    public AjaxResult getAllRooms() {
        QueryWrapper<Room> wrapper = new QueryWrapper<>();
        wrapper.eq("is_use", 0);
        List<Room> rooms = roomService.list(wrapper);

        return rooms == null || rooms.isEmpty()
                ? AjaxResult.fail("未查询到可用的会议室")
                : AjaxResult.success(rooms);
    }


    @GetMapping("/page")
    public AjaxResult getAllRoomsByPage(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<Room> page = new Page<>(pageNum, pageSize);
        Page<Room> roomPage = roomService.page(page);
        return AjaxResult.success(roomPage);
    }


    @GetMapping("/{id}")
    public AjaxResult getRoomById(@PathVariable Integer id) {
        Room room = roomService.getById(id);
        return room != null ? AjaxResult.success(room) : AjaxResult.fail("会议室不存在");
    }

    @PostMapping
    public AjaxResult saveRoom(@RequestBody Room room) {
        boolean saved = roomService.save(room);
        return saved ? AjaxResult.success("新增会议室成功") : AjaxResult.fail("新增失败");
    }

    @PutMapping("/{id}")
    public AjaxResult updateRoom(@PathVariable Integer id, @RequestBody Room room) {
        room.setRoomId(id);
        boolean updated = roomService.updateById(room);
        return updated ? AjaxResult.success("更新成功") : AjaxResult.fail("更新失败");
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteRoomById(@PathVariable Integer id) {
        Room room = roomService.getById(id);
        if (room == null) {
            return AjaxResult.fail("会议室不存在");
        }

        room.setIsUse(1); // 设置为已删除状态（逻辑删除）
        boolean updated = roomService.updateById(room);

        return updated ? AjaxResult.success("删除成功") : AjaxResult.fail("删除失败");
    }


    @PostMapping("/search")
    public AjaxResult selectRoom(@RequestBody RoomSelect roomSelect){
        List<Room> availableRooms = roomService.findAvailableRooms(roomSelect);
        availableRooms = availableRooms.stream().filter(room -> room.getIsUse() == 0).collect(Collectors.toList());
        return AjaxResult.success(availableRooms);
    }


    @PostMapping("/page/condition")
    public AjaxResult pageByCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestBody Room room) {

        if (room.getIsUse() == null) {
            room.setIsUse(0);
        }

        Page<Room> page = new Page<>(pageNum, pageSize);
        IPage<Room> result = roomService.pageByCondition(page, room);

        return AjaxResult.success(result);
    }


    @GetMapping("/now")
    public AjaxResult getCurrentRoomsStatus() {
        List<NowRoom> nowRooms = roomService.getCurrentRoomsStatus();
        return AjaxResult.success(nowRooms);
    }
    @GetMapping("/hourly/status/{roomId}")
    public AjaxResult getHourlyStatus(@PathVariable Integer roomId) {
        List<HourRoom> hourlyStatusList = roomService.getHourlyStatus(roomId);
        return AjaxResult.success(hourlyStatusList);
    }

    @GetMapping("/search/roomid")
    public AjaxResult searchRoomIdsByRoomName(@RequestParam String roomname) {
        List<Map<String, Object>> result = roomService.getRoomIdsByRoomName(roomname);
        if (result == null || result.isEmpty()) {
            return AjaxResult.fail("未找到匹配的会议室");
        }
        return AjaxResult.success(result);
    }
    @GetMapping("/sortedByRating")
    public AjaxResult getAllRoomsSortedByRating() {
        List<Room> rooms = roomService.listRoomsSortedByRating("DESC");
        return rooms == null || rooms.isEmpty() ?
                AjaxResult.fail("未查询到会议室") :
                AjaxResult.success(rooms);
    }
}




